package com.itboye.textileexpres.main;

import java.util.Observable;
import java.util.Observer;

import android.annotation.SuppressLint;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.itboye.textileexpres.R;
import com.itboye.textileexpres.base.BaseActivity;
import com.itboye.textileexpres.base.SpPublicParams;
import com.itboye.textileexpres.guide.SplashActivity;
import com.itboye.textileexpres.login.LoginActivity;
import com.itboye.textileexpres.util.SPUtils;

@SuppressLint("NewApi")
public class MainActivity extends BaseActivity implements Observer,
		OnClickListener, android.widget.CompoundButton.OnCheckedChangeListener {

	private Button btnTest;
	int temp;
	public static int WIDTH;
	// ACache aCache;// 缂撳瓨绫�
	RadioGroup group;
	public Fragment[] fr;
	private RadioButton[] mTabs;
	private int index;
	private int mCurrIndex;
	// int count;
	// ImageView zengwuxian_img;
	String uid;

	private final static String TAGS[] = new String[] { "near", "conv",
			"paotui", "gouwuhce", "wo" }; // fragments鐨則ag
	private final static int Tab_Cnt = 5; // 5涓猼ab椤�

	public void generate() {
		int baseW = 640;
		int baseH = 960;
		String addition = "";

	}

	BroadcastReceiver receiver = new BroadcastReceiver() {

		@Override
		public void onReceive(Context context, Intent intent) {
			int selectIndex = intent.getIntExtra("index", 0);
			System.out.println("selectIndex" + selectIndex);
			index = selectIndex;
			temp = index;
			setCurrentTab();
		}
	};

	@Override
	public void onMyCreate() {
		// RelativeLayout fragment_container =
		startActivity(new Intent(this, SplashActivity.class));
		try {
			uid = (String) SPUtils.get(this, null, SpPublicParams.UID, "");
		} catch (Exception e) {
			// TODO: handle exception
			uid = "";
		}

		WIDTH = getWindowManager().getDefaultDisplay().getWidth();
		IntentFilter filter = new IntentFilter("gotoshopcart");
		registerReceiver(receiver, filter);
		generate();
		initUI();
		initFragments();
	}

	@Override
	public void onSaveInstanceState(Bundle outState,
			PersistableBundle outPersistentState) {
		// TODO Auto-generated method stub
		super.onSaveInstanceState(outState, outPersistentState);
	}

	@Override
	public int initView() {
		// TODO Auto-generated method stub
		return R.layout.activity_main;
	}

	@Override
	protected void onSaveInstanceState(Bundle arg0) {
		// TODO Auto-generated method stub
		// super.onSaveInstanceState(arg0);涓嶆敞閲婃帀锛屼竴鏃︾▼搴忓嚭鐜癰ug,鐣岄潰灏嗗嚭鐜伴噸鍙�
	}

	/*
	 * @see java.util.Observer#update(java.util.Observable, java.lang.Object)
	 * 2016-4-23 hebidu
	 */
	@Override
	public void update(Observable observable, Object data) {
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		// switch (v.getId()) {

		// break;
		// default:
		// break;
		// }

	}

	private void initUI() {
		// tab_radiogroup = (RadioGroup) findViewById(R.id.tab_radiogroup);
		mTabs = new RadioButton[Tab_Cnt];
		mTabs[0] = (RadioButton) findViewById(R.id.radio_button0);
		mTabs[1] = (RadioButton) findViewById(R.id.radio_button1);
		mTabs[2] = (RadioButton) findViewById(R.id.radio_button2);
		mTabs[3] = (RadioButton) findViewById(R.id.radio_button3);
		mTabs[4] = (RadioButton) findViewById(R.id.radio_button4);

		for (RadioButton bt : mTabs) {
			bt.setOnCheckedChangeListener(this);
		}

	}

	private void setCurrentTab() {
		if (mCurrIndex != index) {
			FragmentTransaction trx = getSupportFragmentManager()
					.beginTransaction();
			trx.hide(fr[mCurrIndex]);
			if (!fr[index].isAdded()) {
				trx.replace(R.id.fragment_container, fr[index]);
			}
			trx.show(fr[index]).commitAllowingStateLoss();
			mTabs[mCurrIndex].setChecked(false);
			mCurrIndex = index;
			if (!mTabs[mCurrIndex].isChecked()) {
				mTabs[mCurrIndex].setChecked(true);// 涓嶄細閲嶅瑙﹀彂setCurrentTab
			}

		}
	}

	void initFragments() {
		// ShopCarFZFragment
		fr = new Fragment[] { new GoodsFragment(), new CollectionFragment(),
				new ShopCart(), new OrderFragment(), new MySetFragment() };
		// if (uid == "" || "".equals(uid)) {
		// getSupportFragmentManager().beginTransaction()
		// .add(R.id.fragment_container, fr[0], TAGS[0])
		// .add(R.id.fragment_container, fr[4], TAGS[4]).hide(fr[1])
		// .hide(fr[2]).hide(fr[3]).hide(fr[4]).show(fr[mCurrIndex])
		// .commit();
		// } else {
		getSupportFragmentManager().beginTransaction()
				.add(R.id.fragment_container, fr[0], TAGS[0])
				.add(R.id.fragment_container, fr[1], TAGS[1])
				.add(R.id.fragment_container, fr[2], TAGS[2])
				.add(R.id.fragment_container, fr[3], TAGS[3])
				.add(R.id.fragment_container, fr[4], TAGS[4]).hide(fr[1])
				.hide(fr[2]).hide(fr[3]).hide(fr[4]).show(fr[mCurrIndex])
				.commit();
		// }

	}

	@Override
	public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
		if (isChecked) {
			switch (buttonView.getId()) {
			case R.id.radio_button0:
				index = 0;
				break;
			case R.id.radio_button1:
				index = 1;
				break;
			case R.id.radio_button2:
				index = 2;
				break;
			case R.id.radio_button3:

//				String uid = (String) SPUtils.get(MainActivity.this, null,
//						SpPublicParams.UID, "");
//				System.out.println("uiduiduid" + uid);
//				if (uid == null || uid == "") {
//					Intent intent = new Intent(MainActivity.this,
//							LoginActivity.class);
//					startActivity(intent);
//				}
				index = 3;
				break;
			case R.id.radio_button4:
				index = 4;
				break;
			default:
				break;
			}
			temp = index;
			setCurrentTab();
		}
	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		unregisterReceiver(receiver);
	}
}
